<?php
/*
SW3
by SCOTT ALLAN WALLICK, http://scottwallick.com/
from PLAINTXT.ORG, http://www.plaintxt.org/

This file is part of SW3.

SW3 is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

SW3 is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for details.

You should have received a copy of the GNU General Public License
along with SW3. If not, see www.gnu.org/licenses/.
*/

// Style sheet links for the blog head
function sw3_stylesheets() {
	$charset = get_bloginfo('charset');
	$directory = get_bloginfo('stylesheet_directory');
	echo "\t" . '<link rel="stylesheet" type="text/css" charset="' . $charset . '" href="' . $directory . '/css/main.css" media="screen,projection" />' . "\n";
	echo "\t" . '<link rel="stylesheet" type="text/css" charset="' . $charset . '" href="' . $directory . '/css/thickbox.css" media="screen,projection" />' . "\n";
	if ( is_page('resume') || is_page('hresume') ) {
		echo "\t" . '<link rel="stylesheet" type="text/css" charset="' . $charset . '" href="' . $directory . '/css/hresume.css" media="screen,projection" />' . "\n";
		echo "\t" . '<link rel="stylesheet" type="text/css" charset="' . $charset . '" href="' . $directory . '/css/hresume-print.css" media="print" />' . "\n";
	}
	echo "\t" . '<link rel="stylesheet" type="text/css" charset="' . $charset . '" href="' . $directory . '/css/print.css" media="print" />';
}

// Script links for the blog footer
function sw3_scripts() {
	$charset = get_bloginfo('charset');
	$directory = get_bloginfo('stylesheet_directory');

	echo "<script type='text/javascript' charset='$charset' src='$directory/js/jquery.pack.js'></script>\n";
	echo "<script type='text/javascript' charset='$charset' src='$directory/js/thickbox.js'></script>\n";

	if ( get_option('sw3_pretty_comments') == 0 ) {
		$textareaId = null;
		if ( is_page('contact') ) {
			echo "<script type='text/javascript' charset='$charset' src='$directory/js/pretty-comments.pack.js'></script>\n";
			$textareaId = "\n\tprettyComments.init('ec_message');";
		} elseif ( is_single() ) {
			echo "<script type='text/javascript' charset='$charset' src='$directory/js/pretty-comments.pack.js'></script>\n";
			$textareaId = "\n\tprettyComments.init('comment');";
		}
	}

	$eHeight = attribute_escape(get_option('sw3_explore_height'));
		if ( empty($eHeight) ) $eHeight = '350';
	$eWidth = attribute_escape(get_option('sw3_explore_width'));
		if ( empty($eWidth) ) $eWidth = '500';
	$eTitle = attribute_escape(get_option('sw3_explore_title'));
		if ( empty($eTitle) ) $eTitle = '';

	$fsHeight = attribute_escape(get_option('sw3_slideshow_height'));
		if ( empty($fsHeight) ) $fsHeight = '500';
	$fsWidth = attribute_escape(get_option('sw3_slideshow_width'));
		if ( empty($fsWidth) ) $fsWidth = '700';

	echo "<script type='text/javascript' charset='$charset'>
// <![CDATA[
$(document).ready(function(){
	$('div#primary,body.page div.entry-content ul.portfolio').hide();
	$('body.page div.entry-content div.portfolio-nav').show();
	$('div#menu').prepend('<a id=\"explore\" class=\"thickbox\" title=\"$eTitle\" href=\"#TB_inline?height=$eHeight&amp;width=$eWidth&amp;inlineId=primary\" accesskey=\"E\">Explore</a>');";

	if ( is_attachment() ) {
		$attachmentThickbox = ',body.attachment div.entry-attachment a';
		$attachmentAddthickbox = ',body.attachment div.entry-attachment a.thickbox';
	}

	echo "\n\t$('div.entry-content a.slideshow$attachmentThickbox').addClass('thickbox');
	$('div.entry-content a.slideshow').each(function() {
		$(this).attr('href', $(this).attr('href') + '?TB_iframe=true&amp;height=$fsHeight&amp;width=$fsWidth');
	});
	tb_init('div#menu a.thickbox,div.entry-content a.slideshow$attachmentAddthickbox');";

	echo $textareaId;

	if ( get_option('sw3_google_syntax') == 0 ) {
		echo "\n\tvar syntaxHighlight = false;
	$('div.entry-content pre.code, div.entry-content code.code').each(function(){
		$(this).addClass('prettyprint');
		syntaxHighlight = true;
	});
	if(syntaxHighlight){
		$.getScript('$directory/js/prettify.pack.js', function(){ prettyPrint() });
	}";
	}

	echo "\n});
// ]]>
</script>";

}

// Function to add the theme options submenu
function sw3_add_menu() {
	add_theme_page( __( 'sw3 Theme Options', 'sw3' ), __( 'Theme Options', 'sw3' ), 'edit_themes', basename(__FILE__), 'sw3_menu' );
}

// In case someone uses SW3 and happens to be generous
if ( !function_exists('plaintxt_theme_donate') ) {
	function plaintxt_theme_donate() {
		$button = '
			<form id="paypal" method="post" action="https://www.paypal.com/cgi-bin/webscr" style="float:left;margin:0.9em 0.5em 0 0;">
				<div id="donate">
					<input name="cmd" type="hidden" value="_s-xclick" />
					<input name="submit" src="https://www.paypal.com/en_US/i/btn/x-click-but04.gif" type="image" alt="' . __( 'PayPal: The safer, easier way to donate!', 'sw3' ) . '" />
					<img src="https://www.paypal.com/en_US/i/scr/pixel.gif" alt="' . __( 'Donate with PayPal', 'sw3' ) . '" width="1" height="1" border="0" />
					<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHXwYJKoZIhvcNAQcEoIIHUDCCB0wCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYBrHajOuRyvSPeEPcQdCACJRju5+pVXCd4DGdveRc8ILlvx1Uk1No03n3gc9WrIV3OAmykmBkZ363INuLMx8xoWCmxU77dL/8W9oUcVkOG9Zm7ytcB/9NwgblR1Q/eBD0kwPp1G67IhAGBtwjsobN9Q1GDjfD8aVxV9Cbi8RPrqcDELMAkGBSsOAwIaBQAwgdwGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIsI1Tiy9XlWWAgbgFxUj2mc6BvDOnzkmyC2VMeLQuFkUT6AbnYYRK2UHH5niEJbwVR0SnUlqMLVzkokEFuoIJjVCT0S96ataYgwUJS3bhJZbRjgLY6bZ2aD/8nYPz+lCgO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GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb22CAQAwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQCBXzpWmoBa5e9fo6ujionW1hUhPkOBakTr3YCDjbYfvJEiv/2P+IobhOGJr85+XHhN0v4gUkEDI8r2/rNk1m0GA8HKddvTjyGw/XqXa+LSTlDYkqI8OwR8GEYj4efEtcRpRYBxV8KxAW93YDWzFGvruKnnLbDAF6VR5w/cCMn5hzGCAZowggGWAgEBMIGUMIGOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxFDASBgNVBAoTC1BheVBhbCBJbmMuMRMwEQYDVQQLFApsaXZlX2NlcnRzMREwDwYDVQQDFAhsaXZlX2FwaTEcMBoGCSqGSIb3DQEJARYNcmVAcGF5cGFsLmNvbQIBADAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMDgwMzI4MTYwNzMxWjAjBgkqhkiG9w0BCQQxFgQUYm+qK/h30zV3Jv/NgquxGiaTvTwwDQYJKoZIhvcNAQEBBQAEgYC0z/vcFRrxLGvSPX7p8fqMk3GBACEXqswRXLLXFf2L9m03NZ9ettoBNpUYptSIiqWJ7y8YOwRWOPYWVMnkfYPKKd/4+n9L2AI1NKpH2KafmC6SgsGBIxuHoErjnDngNc+8RPDe3KHAqinnrhN//fEcnxoUxeEtZFJuNSRYJLYNGw==-----END PKCS7-----" />
				</div>
			</form>';
		echo $button . "\n";
	}
}

// Produces the SW3  theme options submenu and saves/resets options
function sw3_menu() {
	if ( !current_user_can('manage_options') ) {
		die( __( '<div id="message" class="error fade"><strong>ACCESS DENIED:</strong> Your don\'t have permission to do this.</div>', 'sw3' ) );
	} elseif ( $_POST['action'] && $_POST['action'] == 'update' ) {
		check_admin_referer('sw3_save_options');
		update_option( 'sw3_explore_height', strip_tags(stripslashes($_POST['explore_height'])) );
		update_option( 'sw3_explore_title', strip_tags(stripslashes($_POST['explore_title'])) );
		update_option( 'sw3_explore_width', strip_tags(stripslashes($_POST['explore_width'])) );
		update_option( 'sw3_google_syntax', strip_tags(stripslashes($_POST['google_syntax'])) );
		update_option( 'sw3_pretty_comments', strip_tags(stripslashes($_POST['pretty_comments'])) );
		update_option( 'sw3_slideshow_height', strip_tags(stripslashes($_POST['slideshow_height'])) );
		update_option( 'sw3_slideshow_width', strip_tags(stripslashes($_POST['slideshow_width'])) );
		echo '<div id="message" class="updated fade"><p><strong>' . __( 'Settings saved.', 'sw3' ) . '</strong></p></div>';
	} elseif ( $_POST['action'] && $_POST['action'] == 'reset' ) {
		check_admin_referer('sw3_reset_options');
		delete_option('sw3_explore_height');
		delete_option('sw3_explore_title');
		delete_option('sw3_explore_width');
		delete_option('sw3_google_syntax');
		delete_option('sw3_pretty_comments');
		delete_option('sw3_slideshow_height');
		delete_option('sw3_slideshow_width');
		echo '<div id="message" class="updated fade"><p><strong>' . __( 'Settings reset.', 'sw3' ) . '</strong></p></div>';
	}
	$exploreHeight = get_option('sw3_explore_height');
	$exploreWidth = get_option('sw3_explore_width');
	$slideshowHeight = get_option('sw3_slideshow_height');
	$slideshowWidth = get_option('sw3_slideshow_width');
?>
<div class="wrap">
	<h2><?php _e( 'sw3 Theme Options', 'sw3' ) ?></h2>
	<?php if ( function_exists('plaintxt_theme_donate') ) plaintxt_theme_donate(); ?>
	<p><?php printf(
		__( 'You are using the sw3 theme template, version %1$s, by <span class="vcard"><a class="url fn n" href="%2$s" title="Scott Allan Wallick" rel="designer"><span class="given-name">Scott</span> <span class="additional-name">Allan</span> <span class="family-name">Wallick</span></a></span>. CorpsLog uses the rich semantic markup of the <a href="%3$s" title="Sandbox for WordPress">Sandbox</a> for its theme files. If you\'re using this theme, you should probably <a href="%4$s/wp-content/themes/sw3/license.txt" title="View license">review its license</a>.', 'sw3' ),
			'0.2.1', // Current SW3 version
			'http://scottwallick.com/', // Our author's personal URI
			'http://www.plaintxt.org/themes/sandbox/', // The SANDBOX theme URI
			get_bloginfo('wpurl') // The directory of our WordPress installation
	) ?></p>
	<form name="sw3_save_options" method="post" action="<?php echo wp_specialchars($_SERVER['REQUEST_URI']) ?>">
		<?php wp_nonce_field('sw3_save_options'); echo "\n"; ?>
		<h3><?php _e( 'Theme options', 'sw3' ) ?></h3>
		<table class="form-table" summary="<?php _e( 'Theme options', 'sw3' ) ?>">
			<tr>
				<th scope="row"><?php _e( 'Explore box dimensions', 'sw3' ) ?></th>
				<td>
					<label for="explore_width"><?php _e( 'Width', 'sw3' ) ?></label>
					<input id="explore_width" name="explore_width" type="text" value="<?php if ( empty($exploreWidth) ) echo '500'; else echo $exploreWidth; ?>" class="text" size="6" />
					<label for="explore_height"><?php _e( 'Height', 'sw3' ) ?></label>
					<input id="explore_height" name="explore_height" type="text" value="<?php if ( empty($exploreHeight) ) echo '350'; else echo $exploreHeight; ?>" class="text" size="6" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="menu_title"><?php _e( 'Explore box title', 'sw3' ) ?></label></th>
				<td><input id="explore_title" name="explore_title" type="text" value="<?php echo attribute_escape(get_option('sw3_explore_title')) ?>" class="text" size="40" /></td>
			</tr>
			<tr>
				<th scope="row"><?php _e( 'Slideshow box dimensions', 'sw3' ) ?></th>
				<td>
					<label for="slideshow_width"><?php _e( 'Width', 'sw3' ) ?></label>
					<input id="slideshow_width" name="slideshow_width" type="text" value="<?php if ( empty($slideshowWidth) ) echo '700'; else echo $slideshowWidth; ?>" class="text" size="6" />
					<label for="slideshow_height"><?php _e( 'Height', 'sw3' ) ?></label>
					<input id="slideshow_height" name="slideshow_height" type="text" value="<?php if ( empty($slideshowHeight) ) echo '500'; else echo $slideshowHeight; ?>" class="text" size="6" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="pretty_comments"><?php _e( 'PrettyComments is', 'sw3' ) ?></label></th>
				<td>
					<select name="pretty_comments" id="pretty_comments">
						<option value="1"<?php if ( get_option('sw3_pretty_comments') == 1 ) echo ' selected="selected"'; ?>> <?php _e( 'Disabled', 'sw3' ) ?> </option>
						<option value="0"<?php if ( get_option('sw3_pretty_comments') == 0 ) echo ' selected="selected"'; ?>> <?php _e( 'Enabled', 'sw3' ) ?> </option>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="google_syntax"><?php _e( 'Highlight.js is', 'sw3' ) ?></label></th>
				<td>
					<select name="google_syntax" id="google_syntax">
						<option value="1"<?php if ( get_option('sw3_google_syntax') == 1 ) echo ' selected="selected"'; ?>> <?php _e( 'Disabled', 'sw3' ) ?> </option>
						<option value="0"<?php if ( get_option('sw3_google_syntax') == 0 ) echo ' selected="selected"'; ?>> <?php _e( 'Enabled', 'sw3' ) ?> </option>
					</select>
				</td>
			</tr>
		</table>
		<p class="submit">
			<input id="update" name="update" type="submit" value="<?php _e( 'Save Changes', 'sw3' ) ?>" accesskey="S" />
			<input name="action" type="hidden" value="update" />
			<input name="page_options" type="hidden" value="menu_height,menu_weight,menu_title,pretty_comments,google_syntax" />
		</p>
	</form>
	<h3 id="reset"><?php _e('Options reset', 'sw3') ?></h3>
	<p><?php _e( 'Resetting deletes all stored sw3 options from your database. After resetting, default options are loaded but are not stored until you click <i>Save Options</i>. A reset does not affect the actual theme files in any way. If you are uninstalling veryplaintxt, please reset before removing the theme files to clear your databse.', 'sw3' ) ?></p>
	<form name="sw3_reset_options" method="post" action="<?php echo wp_specialchars($_SERVER['REQUEST_URI']) ?>">
		<?php wp_nonce_field('sw3_reset_options'); echo "\n"; ?>
		<p class="submit">
			<input id="reset" name="reset" type="submit" value="<?php _e( 'Reset Options', 'sw3' ) ?>" onclick="return confirm('<?php _e( 'Click OK to reset. Any changes to these theme options will be lost!', 'sw3' ) ?>');" accesskey="R" />
			<input name="action" type="hidden" value="reset" />
			<input name="page_options" type="hidden" value="explore_height,explore_weight,explore_title,pretty_comments,google_syntax,slideshow_height,slideshow_width" />
		</p>
	</form>
</div>
<?php
}

// Initialize the SW3 theme options menu after activating
add_action( 'admin_menu', 'sw3_add_menu' );

// Adds the function for conditional style sheet links to blog head
add_action( 'wp_head', 'sw3_stylesheets', 1 );

// Adds the function for javascript links to blog footer
add_action( 'wp_footer', 'sw3_scripts', 1 );

// Loads localizations, if applicable (which it won't be)
load_theme_textdomain('sw3');

// End SW3 functions
?>